package com.song.login.controller;


import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/login")
public class LoginController {
	
	
	@RequestMapping("/login")
	public String Login()
	{
		return "/test";
	}
	
	@RequestMapping("/doLogin")
	public String doLogin(@RequestParam(value="username" ,required=false) String username, @RequestParam(value="password" ,required=false) String password)
	{
		if(StringUtils.isBlank(username))
		{
			return "/login";
		}
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		boolean flag = true;
		try {
			subject.login(token);
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = false;
		}
		if(flag)
		{
			return "/index";
		}else{
			return "/login";
		}
		
	}
	
	
	@RequestMapping("/index")
	public String index()
	{
		return "/index";
	}
	
	
}
